package com.zzw.controller;

import com.zzw.entity.*;
import com.zzw.page.Page;
import com.zzw.service.*;
import com.zzw.entity.CurrentUser;
import com.zzw.utils.TokenUtils;
import com.zzw.utils.WarehouseConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Project: warehouse
 * @Description:
 * @Author: zzw
 */
@RestController
@RequestMapping("/product")
@Slf4j
@Api(tags = "商品模块")
public class ProductController {

    @Autowired
    private StoreService storeService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private PlaceService placeService;

    @Autowired
    private ProductService productService;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private SupplyService supplyService;

    @Autowired
    private UnitService unitService;


    /**
     * 查询所有仓库的url接口
     * 返回值Result对象给客户端响应查询到的List<Store>;
     */
    @GetMapping("/store-list")
    public Result storeList() {
        //执行业务
        List<Store> storeList = storeService.queryAllStore();
        //响应
        return Result.ok(storeList);
    }

    /**
     * 查询所有品牌的url接口
     * 返回值Result对象给客户端响应查询到的List<Brand>;
     */
    @GetMapping("/brand-list")
    public Result brandList() {
        //执行业务
        List<Brand> brandList = brandService.queryAllBrand();
        //响应
        return Result.ok(brandList);
    }

    /**
     * 查询所有商品分类树的url接口/product/category-tree
     * <p>
     * 返回值Result对象给客户端响应查询到的所有商品分类树List<ProductType>;
     */
    @GetMapping("/category-tree")
    public Result categoryTree() {
        //执行业务
        List<ProductType> typeTreeList = productTypeService.allProductTypeTree();
        //响应
        return Result.ok(typeTreeList);
    }

    /**
     * 分页查询商品的url接口
     *
     * @param page
     * @param product
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询商品")
    public Result productPageList(Page page, Product product) {
        page = productService.queryProductPage(page, product);
        return Result.ok(page);
    }

    /**
     * 查询所有供应商的url接口
     * 返回值Result对象给客户端响应查询到的List<Supply>;
     */
    @GetMapping("/supply-list")
    public Result supplyList() {
        //执行业务
        List<Supply> supplyList = supplyService.queryAllSupply();
        //响应
        return Result.ok(supplyList);
    }

    /**
     * 查询所有产地的url接口/product/place-list
     * 返回值Result对象给客户端响应查询到的List<Place>;
     */
    @GetMapping("/place-list")
    public Result placeList() {
        //执行业务
        List<Place> placeList = placeService.queryAllPlace();
        //响应
        return Result.ok(placeList);
    }

    /**
     * 查询所有单位的url接口/product/unit-list
     * 返回值Result对象给客户端响应查询到的List<Unit>;
     */
    @GetMapping("/unit-list")
    public Result unitList() {
        //执行业务
        List<Unit> unitList = unitService.queryAllUnit();
        //响应
        return Result.ok(unitList);
    }

    /**
     * 将配置文件的file-upload-path属性值注入给控制器的uploadPath属性,
     * 其为图片上传到项目的目录路径(类路径classes即resources下的static/img/upload);
     */
    @Value("${file.upload-path}")
    private String uploadPath;

    /**
     * 上传图片的url接口
     * 参数MultipartFile file对象封装了上传的图片;
     *
     * @param file
     * @return
     * @CrossOrigin 表示该url接口允许跨域请求;
     */
    @CrossOrigin
    @PostMapping("/img-upload")
    @ApiOperation("上传图片")
    public Result uploadImg(MultipartFile file) {

        try {
            //拿到图片上传到的目录的File对象
            File uploadDirFile = ResourceUtils.getFile(uploadPath);
            //拿到图片上传的目录的磁盘路径
            String uploadDirPath = uploadDirFile.getAbsolutePath();
            //拿到图片保存到的磁盘路径
            String fileUploadPath = uploadDirPath + "\\" + file.getOriginalFilename();
            //保存图片
            file.transferTo(new File(fileUploadPath));
            //成功响应
            return Result.ok("图片上传成功！");
        } catch (IOException e) {
            //失败响应
            return Result.err(Result.CODE_ERR_BUSINESS, "图片上传失败！");
        }
    }

    /**
     * 添加商品的url接口
     *
     * @param product
     * @param token
     * @return
     */
    @PostMapping
    @ApiOperation("添加商品")
    public Result saveProduct(@RequestBody Product product, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {

        //获取当前登录用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录用户的id,即添加商品的用户id
        int createBy = currentUser.getUserId();
        product.setCreateBy(createBy);

        //执行业务
        return productService.saveProduct(product);
    }

    /**
     * 修改商品上下架状态的url接口
     *
     * @param product
     * @return
     */
    @PutMapping("/state")
    @ApiOperation("修改商品状态")
    public Result changeProductState(@RequestBody Product product) {
        return productService.updateProductState(product);
    }

    /**
     * 修改商品的url接口
     *
     * @param product
     * @param token
     * @return
     */
    @PutMapping
    @ApiOperation("修改商品")
    public Result updateProduct(@RequestBody Product product,
                                @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {

        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录用户的id,即修改商品的用户id
        int updateBy = currentUser.getUserId();
        product.setUpdateBy(updateBy);

        return productService.updateProduct(product);
    }

    /**
     * 删除商品的url接口
     *
     * @param productId
     * @return
     */
    @DeleteMapping("/{productId}")
    @ApiOperation("删除商品")
    public Result deleteProduct(@PathVariable Integer productId) {
        return productService.deleteProduct(productId);
    }

    //导出数据 get exportTable

    //批量删除商品 del list
    //批量删除商品
    @DeleteMapping("/list")
    public Result deleteProductList(@RequestBody List<Integer> productIdList) {
        return productService.deleteProductByIds(productIdList);
    }

    /**
     * 导出数据
     *
     * @param page
     * @param product
     * @return
     */
    @GetMapping("/exportTable")
    public Result exportTable(Page page, Product product) {
        page = productService.queryProductPage(page, product);
        List<?> resultList = page.getResultList();
        //响应
        return Result.ok(resultList);
    }
}


